﻿using System;
using System.Globalization;

namespace Calendar.Logic.ObjectModel
{
    [AttributeUsage(AttributeTargets.Property)]
    public class MapContractAttribute : Attribute
    {
        public MapContractAttribute(string name)
        {
            Name = name;
        }

        public string Name { get; private set; }
    }

    [Serializable]
    public class SerializableContract
    {
        public string Start { get; set; }
        public string End { get; set; }
        public string Title { get; set; }
        public string TenderName { get; set; }
        public int EntityId { get; set; }

        public SerializableContract(object obj)
        {
            var culture = CultureInfo.CreateSpecificCulture("en-US");

            EntityId = (int)obj.GetType().GetProperty("ID").GetValue(obj, null);

            foreach (var member in obj.GetType().GetMembers())
            {
                foreach (var attribute in member.GetCustomAttributes(true))
                {
                    if (attribute.GetType() != typeof (MapContractAttribute)) continue;

                    var property = obj.GetType().GetProperty(member.Name).GetValue(obj, null);
                    switch (((MapContractAttribute)attribute).Name)
                    {
                        case "Start":
                            Start = property != null ? ((DateTime)property).ToString("MM/dd/yyyy", culture) : string.Empty;
                            break;
                        case "End":
                            End = property != null ? ((DateTime)property).ToString("MM/dd/yyyy", culture) : string.Empty;
                            break;
                        case "Title":
                            Title = property.ToString();
                            break;
                    }
                }
            }
        }

    }
}
